package F21AS_Freddy.AFJ_Taxikiosk.src;


import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

/**
 * This class used to control KIOSK 
 * @author Freddy Wong
 * F21AS Course Work Stage2 @ Year 2015
 */
public class KioskController {

	private Kiosk kiosk;

	public KioskController(Kiosk kiosk, KioskGUI kioskGUI) {

		this.kiosk = kiosk;
		kioskGUI.setVisible(true);
		kioskGUI.addCloseKioskButtonListener(new CloseKioskButtonListener());

		// open the desk after a random delay - run in a separate thread
		Thread thread = new Thread(new openKiosk(kiosk));
		thread.start();

	}

	// private class to open the drop-off desk after a random delay
	private class openKiosk implements Runnable {
		static final int DELAY_START = 7000;
		Kiosk kiosk;

		openKiosk(Kiosk kiosk) {
			this.kiosk = kiosk;
		}

		public void run() {
			try {
				// wait a random time between 0 and 10s
				Random random = new Random();
				Thread.sleep(random.nextInt(DELAY_START));
				// open the drop-off desk
				kiosk.setIsOpen(true);
				// start processing the queue
				kiosk.processQueue();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}

	// Handle the button event on the GUI and close the drop-off desk
	public class CloseKioskButtonListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			kiosk.setIsOpen(false);

		}
	}

} // End of KioskController Class
